﻿using Abp.Application.Services;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.AutoMapper;
using Microsoft.AspNet.Identity;
using RMM.Authorization;
using RMM.Authorization.Tenants;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RMM.Orders.Dtos;
using RMM.Core.Orders;
using RMM.Shops;
using RMM.UserAdminService;

namespace RMM.AdminOrderService
{
    //[AbpAuthorize]
    public class OrderAdminService : ApplicationService, IOrderAdminService
    {
        private readonly IOrderService _orderService;
        private readonly IOrderItemService _orderItemsService;
        private readonly IShopProjectAppService _shopProjectAppService;
        private readonly IUserAdminService _userAdminService;


        public OrderAdminService(IOrderService orderService
            , IShopProjectAppService shopProjectAppService
            , IOrderItemService orderItemsService
            ,IUserAdminService userAdminService)
        {
            _orderService = orderService;
            _shopProjectAppService = shopProjectAppService;
            _orderItemsService = orderItemsService;
            _userAdminService = userAdminService;
        }



        public async Task<List<AdminOrderDto>> GetOrderlistAsync()
        {
            var dto = new List<AdminOrderDto>();
            var order = await _orderService.GetOrderListAsync();
            foreach (var item in order)
            {
                var orderItem = await _orderItemsService.FirstOrDefaultAsync(p => p.OrderId == item.Id);
                var project = await _shopProjectAppService.GetProjectById((int)orderItem.ProjectId);
                var username = await _userAdminService.GetUserByidAsync((int)item.UserId);
                foreach (var dtos in dto)
                {
                    dtos.ProjectName = project.ProjectName;
                    dtos.UserName = username.UserName;
                }
            }
            dto = order.MapTo<List<AdminOrderDto>>();

            return dto;
        }



        //public async Task<ResultDto> CreateOrderAsync(CreateOrderDto createOrderDto)
        //{
        //    var shopProject = await _shopProjectAppService.GetProjectById(createOrderDto.ProjectId);
        //    if (shopProject == null)
        //    {
        //        return ResultDto.Fail("项目不存在");
        //    }


        //    var order = createOrderDto.MapTo<Order>();
        //    var minute = order.ServerEndDate.Value.Subtract(order.ServerStartDate.Value).Duration().Minutes;

        //    order.Total = shopProject.Price / shopProject.ProjectTime * minute;
        //    order.Price = order.Total;
        //    order.CreatorUserId = createOrderDto.UserId;
        //    order.CreationTime = DateTime.Now;
        //    order.OrderCode = Guid.NewGuid().ToString();
        //    order.UserId = abpSession.UserId ?? 0;
        //    //折扣
        //    if (CommonHelper.Check(createOrderDto.Discount))
        //    {
        //        order.Discount = createOrderDto.Discount.Value;
        //        order.Price = order.Price - order.Discount;
        //    }
        //    //优惠券
        //    if (CommonHelper.Check(createOrderDto.Coupon))
        //    {
        //        order.Coupon = createOrderDto.Coupon.Value;
        //        order.Price = order.Price - order.Coupon;
        //    }
        //    //完成后算人数
        //    order.Price = order.Price * createOrderDto.Num;

        //    var orderId = await CreateOrderAndGetIdAsync(order);

        //    await EventBus.TriggerAsync(new OrderItemEventData()
        //    {
        //        order = order
        //    });

        //    return ResultDto.Successed;
        //}

        //private async Task<long> CreateOrderAndGetIdAsync(Order order)
        //{
        //    var result = await _orderService.CreateAndGetIdAsync(order);
        //    return result.EntityId.Value;
        //}

    }
}
